module libeventd.buffer.event.Internal ;

import
	libeventd.Types;

extern(C):

enum{
	BEV_SUSPEND_WM =0x01,
	BEV_SUSPEND_BW =0x02,
	BEV_SUSPEND_BW_GROUP =0x04,
	BEV_SUSPEND_LOOKUP =0x08,
}

struct bufferevent_rate_limit_group {
	rlim_group_member_list members;

	ev_token_bucket		rate_limit;
	ev_token_bucket_cfg	rate_limit_cfg;
	
	ubyte[4]	__data ;
	/*
	unsigned read_suspended : 1;
	unsigned write_suspended : 1;
	unsigned pending_unsuspend_read : 1;
	unsigned pending_unsuspend_write : 1;
	*/

	ev_uint64_t	total_read;
	ev_uint64_t 	total_written;

	ev_int		n_members;
	ev_uint32_t	min_share;
	
	event		master_refill_event;
	void*		lock;
}

struct bufferevent_rate_limit {
	TAILQ_ENTRY!(bufferevent_private)	next_in_group;
	bufferevent_rate_limit_group*		group;
	ev_token_bucket					limit;
	ev_token_bucket_cfg*				cfg;
	event						refill_bucket_event;
}

struct bufferevent_private {
	
	bufferevent bev;

	evbuffer_cb_entry *read_watermarks_cb;

	ubyte[4]	__data;
	/*
	unsigned own_lock : 1;
	unsigned readcb_pending : 1;
	unsigned writecb_pending : 1;
	unsigned connecting : 1;
	unsigned connection_refused : 1;
	*/
	
	ev_short eventcb_pending;
	ev_short read_suspended;
	ev_short write_suspended;
	ev_int errno_pending;
	ev_int dns_error;
	deferred_cb deferred;
	bufferevent_options options;
	ev_int refcnt;
	void *lock;
	bufferevent_rate_limit *rate_limiting;
}

struct rlim_group_member_list{
	mixin TAILQ_HEAD!(bufferevent_private) ;
}


enum bufferevent_ctrl_op {
	BEV_CTRL_SET_FD,
	BEV_CTRL_GET_FD,
	BEV_CTRL_GET_UNDERLYING
}

union bufferevent_ctrl_data {
	void*	ptr;
	evutil_socket_t	fd;
}


struct bufferevent_ops {
	char*	type;
	off_t		mem_offset;
	ev_int (*enable)(bufferevent *, ev_short);
	ev_int (*disable)(bufferevent *, ev_short);
	void (*destruct)(bufferevent *);
	ev_int (*adj_timeouts)(bufferevent *);
	ev_int (*flush)(bufferevent *, ev_short, bufferevent_flush_mode);
	ev_int (*ctrl)(bufferevent *, bufferevent_ctrl_op, bufferevent_ctrl_data *);
}